package com.example.springboot_manage.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.springboot_manage.util.JsonUtil;
import com.example.springboot_manage.repository.GoodsCategoryMapper;
import com.example.springboot_manage.pojo.GoodsCategory;
import com.example.springboot_manage.pojo.GoodsCategoryExample;
import com.example.springboot_manage.service.GoodsCategoryService;
import com.example.springboot_manage.vo.GoodsCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Value("goods.category.list.key")
    private String goodsCategoryListKey;
    /**
     * 新增分类-查询所有顶级分类(parentId=0)
     * @return
     */
    @Override
    public List<GoodsCategory> selectCategoryTopList() {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andParentIdEqualTo((short) 0);
        return goodsCategoryMapper.selectByExample(example);
    }

    /**
     * 新增分类-根据父id查询子分类
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsCategory> selectCategoryListByParentId(short parentId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return goodsCategoryMapper.selectByExample(example);
    }

    /**
     * 新增分类-查询商品分类（通用）
     * @param parentId
     * @return
     */
    @Override
    public List<GoodsCategory> selectCategoryList(short parentId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        return goodsCategoryMapper.selectByExample(example);
    }

    @Override
    public int categorySave(GoodsCategory goodsCategory) {
        //删除redis缓存
        redisTemplate.delete(redisTemplate.keys("goods*"));
        return goodsCategoryMapper.insertSelective(goodsCategory);
    }

    @Override
    public List<GoodsCategoryVO> selectCategoryForView() {
        //Redis缓存操作
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        //查看缓存中是否有，没有则查询数据库
        String gcvListJson = valueOperations.get(goodsCategoryListKey);
        if (!StringUtils.isEmpty(gcvListJson)){
            return JsonUtil.jsonToList(gcvListJson,GoodsCategoryVO.class);
        }



        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andParentIdEqualTo((short) 0).andLevelEqualTo((byte) 1);
        List<GoodsCategory> list = goodsCategoryMapper.selectByExample(example);
        List<GoodsCategoryVO> gcVOs = new ArrayList<>();
        for (GoodsCategory goodsCategory : list) {
            GoodsCategoryVO gcVO = new GoodsCategoryVO();
            BeanUtils.copyProperties(goodsCategory,gcVO);
            example.clear();
            example.createCriteria().andParentIdEqualTo(goodsCategory.getId()).andLevelEqualTo((byte) 2);
            //查询二级分类
            List<GoodsCategory> list2 = goodsCategoryMapper.selectByExample(example);
            List<GoodsCategoryVO> gcVOs2 = new ArrayList<>();
            for (GoodsCategory category : list2) {
                GoodsCategoryVO gcVO2 = new GoodsCategoryVO();
                BeanUtils.copyProperties(category,gcVO2);
                example.clear();
                example.createCriteria().andParentIdEqualTo(category.getId()).andLevelEqualTo((byte) 3);
                //查询三级分类
                List<GoodsCategory> gcList3 = goodsCategoryMapper.selectByExample(example);
                List<GoodsCategoryVO> gcVOs3 = new ArrayList<>();
                for (GoodsCategory goodsCategory3 : gcList3) {
                    GoodsCategoryVO gcVO3 = new GoodsCategoryVO();
                    BeanUtils.copyProperties(goodsCategory3,gcVO3);
                    gcVOs3.add(gcVO3);
                }
                gcVO2.setChildren(gcVOs3);
                gcVOs2.add(gcVO2);
            }
            gcVO.setChildren(gcVOs2);
            gcVOs.add(gcVO);
        }
        //放入Redis缓存
        valueOperations.set(goodsCategoryListKey, JsonUtil.object2JsonStr(gcVOs));
        return gcVOs;
    }

    @Override
    public List<GoodsCategory> selectCategoryList() {
        return goodsCategoryMapper.selectByExample(new GoodsCategoryExample());
    }
}
